#!/bin/bash

# 需要安装 xvfb_1.20.13-1ubuntu1~20.04.8_amd64.deb包
# 包地址：ftp：/sdk_docker/xvfb_1.20.13-1ubuntu1~20.04.8_amd64.deb
# 安装命令
# sudo dpkg -i xvfb_1.20.13-1ubuntu1~20.04.8_amd64.deb

#ftp info
WORK_PATH=$(readlink -f ${0} | xargs dirname)
FTP_IP="10.1.19.164"
FTP_PORT="21"
FTP_NAME="tagldev"
FTP_PASS="arene_IVI-=00"
FTP_SDK_DOCKER_URL="ftp://tagldev:arene%5FIVI-%3D00@10.1.19.164:${FTP_PORT}/sdk_docker"

#variable for sdk
ENV_PATH="${WORK_PATH}/env"
SDK_TARGET_PATH="${WORK_PATH}/env/sdk"
SDK_TARGET_CONF="sdk.conf"
SDK_VERSION_FILE="${SDK_TARGET_PATH}/version-core2-64-arene-linux"
DOCKER_TARGET_PATH="${WORK_PATH}/env/arene-ivi-misc-configs/sdk-hal-docker"
DOCKER_SCRIPT_PATH="${WORK_PATH}/env/arene-ivi-misc-configs"
DOCKER_SCRIPT="${DOCKER_SCRIPT_PATH}/arene-ivi_docker.sh"
DEFAULT_POKY_CONTAINER="arene/poky-container:ubuntu-20.04"

arene_ivi_misc_config_tar="arene-ivi-misc-configs.tar.gz"

FTP_BEV_URL="ftp://tagldev_doc:arene%5FIVI-%3D00@${FTP_IP}:${FTP_PORT}"
FTP_BEV_RELEASE_URL="${FTP_BEV_URL}/03.BEV/sdk_docker"
DOCKER_BEV_PATH="${DOCKER_SCRIPT_PATH}/sdk-hal-docker-cdc"
DOCKER_BEV_SCRIPT="${DOCKER_SCRIPT_PATH}/arene-cdc_docker.sh"
BEV_HAL_CONTAINER_NAME="sdk-hal-docker-cdc_app_1"
BEV_RUNTIMEL_CONTAINER_NAME="sdk-hal-docker-cdc_arene_1"
USER_PASSWORD="asdf1234"
########################################################################################################################

function log()
{
    echo "----------------------------------------------------------------------------------------------------------------"
    echo ${1}
    echo "----------------------------------------------------------------------------------------------------------------"
}


function install_sdk() {
    if [ -e ${ENV_PATH} ]; then
        log "[NORMAL] ----[${ENV_PATH}] existed !!!----"
    else
        mkdir ${ENV_PATH}
        log "[NORMAL] --create ${ENV_PATH} OK!"
    fi
    # check sdk version
    # if [ -f $SDK_VERSION_FILE ] && [ $(grep -rn ${SDK_VERSION} $SDK_VERSION_FILE | wc -l) -gt 0 ]; then
    #     echo "SDK is installed , re-install !"
	#     #return
    # fi
    
    cd ${ENV_PATH}
    if [[ ${1} ]];then
        SDK_NAME=arene-toolchain-cockpit-ivi-${1}-genericx86-64.sh
    else
        log "[NORMAL ] rm -f ${SDK_TARGET_CONF} ."
        rm -f ${SDK_TARGET_CONF}

        log "[NORMAL ] wget -O ${SDK_TARGET_CONF} ${FTP_SDK_DOCKER_URL}/${SDK_TARGET_CONF} ."
        wget -O ${SDK_TARGET_CONF} ${FTP_SDK_DOCKER_URL}/${SDK_TARGET_CONF}
        if [[ $?  -ne 0 ]]; then
            log "[ERROR ] wget -O ${SDK_TARGET_CONF} ${FTP_SDK_DOCKER_URL}/${SDK_TARGET_CONF} ERROR !!!."
            return 1
        fi
        SDK_VERSION=$(grep SDK_VERSION sdk.conf | cut -d = -f 2)
        if [[ ${SDK_VERSION} ]];then
            SDK_NAME=arene-toolchain-cockpit-ivi-${SDK_VERSION}-genericx86-64.sh
        else
            log "[ERROR ] SDK_VERSION=[${SDK_VERSION}] ERROR !!!."
            return 1
        fi
    fi
    
    # download sdk
    log "[NORMAL ] rm -f ${SDK_NAME} ."
    rm -f ${SDK_NAME}

    log "[NORMAL ] wget -O ${SDK_NAME} ${FTP_SDK_DOCKER_URL}/${SDK_NAME} ."
    wget -O ${SDK_NAME} ${FTP_SDK_DOCKER_URL}/${SDK_NAME}
    if [[ $?  -ne 0 ]]; then
        log "[ERROR ] wget SDK File ERROR !!!."
        return 1
    fi
    chmod +x ${SDK_NAME}

    # remove old sdk
    log "[NORMAL ] rm -f ${SDK_TARGET_PATH} ."
    rm -rf ${SDK_TARGET_PATH}

    #install sdk
    cd ${WORK_PATH}
    log "[NORMAL ] ------ install sdk start ------."
    docker run --rm -v ~:/home/hosthome:ro -v $(pwd):/workdir \
        --name poky-$(id -un) \
        --workdir=/workdir \
        ${DEFAULT_POKY_CONTAINER} ./env/${SDK_NAME} -y -d ./env/sdk

    grep -rni "version" ${SDK_VERSION_FILE}
    grep -rni "SDK_DATE_VERSION" ${ENV_PATH}/${SDK_TARGET_CONF}

}

function install_local_sdk() {
    if [ -e ${ENV_PATH} ]; then
        log "[NORMAL] ----[${ENV_PATH}] existed !!!----"
    else
        mkdir ${ENV_PATH}
        log "[NORMAL] --create ${ENV_PATH} OK!"
    fi
    # check sdk version
    # if [ -f $SDK_VERSION_FILE ] && [ $(grep -rn ${SDK_VERSION} $SDK_VERSION_FILE | wc -l) -gt 0 ]; then
    #     echo "SDK is installed , re-install !"
	#     #return
    # fi
    
    cd ${ENV_PATH}

    SDK_VERSION=$(grep SDK_VERSION sdk.conf | cut -d = -f 2)
    if [[ ${SDK_VERSION} ]];then
        SDK_NAME=arene-toolchain-cockpit-ivi-${SDK_VERSION}-genericx86-64.sh
    else
        log "[ERROR ] SDK_VERSION=[${SDK_VERSION}] ERROR !!!."
        return 1
    fi
    
    if [[ -e ${SDK_NAME} ]];then
        chmod +x ${SDK_NAME}
    else
        log "[ERROR ] ${SDK_NAME} does not exist !!!."
        return 1
    fi 

    # remove old sdk
    log "[NORMAL ] rm -f ${SDK_TARGET_PATH} ."
    rm -rf ${SDK_TARGET_PATH}

    #install sdk
    cd ${WORK_PATH}
    log "[NORMAL ] ------ install sdk start ------."
    docker run --rm -v ~:/home/hosthome:ro -v $(pwd):/workdir \
        --name poky-$(id -un) \
        --workdir=/workdir \
        ${DEFAULT_POKY_CONTAINER} ./env/${SDK_NAME} -y -d ./env/sdk

    grep -rni "version" ${SDK_VERSION_FILE}
    grep -rni "SDK_DATE_VERSION" ${ENV_PATH}/${SDK_TARGET_CONF}

}

function download_source() {
    local isNeedFetchCode=true
    if [[ -d "$SRC_PATH" ]]; then
        local git_dir=$SRC_PATH"/.git"
        if [[ -d "$git_dir" ]]; then
            cd $SRC_PATH
            echo $(git pull)
            cd $ARENE_WORKSPACE
            isNeedFetchCode=false
        fi
    fi

    # clone git repo.
    if [ "${isNeedFetchCode}" ]; then
        cd $ARENE_WORKSPACE
        rm -rf $SRC_PATH
        # build container
        echo $(git clone "$SRC_REPO" "$SRC_PATH")
        cd $ARENE_WORKSPACE 
    fi
}

function install_docker_env() {
    if [ -e ${ENV_PATH} ]; then
        log "[NORMAL] ----[${ENV_PATH}] existed !!!----"
    else
        mkdir ${ENV_PATH}
        log "[NORMAL] --create ${ENV_PATH} OK!"
    fi

    log "[NORMAL ] rm -rf ${ENV_PATH}/arene-ivi-misc-configs*."
    rm -rf ${ENV_PATH}/arene-ivi-misc-configs*

    log "[NORMAL ] wget -O ${ENV_PATH}/${arene_ivi_misc_config_tar} ${FTP_SDK_DOCKER_URL}/${arene_ivi_misc_config_tar}."
    wget  -O ${ENV_PATH}/${arene_ivi_misc_config_tar} ${FTP_SDK_DOCKER_URL}/${arene_ivi_misc_config_tar}
    if [[ $?  -ne 0 ]]; then
        log "[ERROR ] wget -O ${ENV_PATH}/${arene_ivi_misc_config_tar} ${FTP_SDK_DOCKER_URL}/${arene_ivi_misc_config_tar} ERROR !!!."
        return 1
    fi
    tar -zxf ${ENV_PATH}/${arene_ivi_misc_config_tar} -C ${ENV_PATH}
    if [[ $?  -ne 0 ]]; then
        log "[ERROR ] tar -zxvf ${ENV_PATH}/${arene_ivi_misc_config_tar} ERROR !!!."
        return 1
    fi

    cd ${DOCKER_TARGET_PATH}
    sed -ri 's/(^DOCKER_IMAGE_HAL_NAME=sdk-docker-hal$)/\1-mock/g' .env
    sed -ri "s@WORK_DIR=~/hmifw_share@WORK_DIR=${WORK_PATH}@g" .env
    sed -ri 's@(^\s+- /dev/dri:/dev/dri$)@#\1@g' ./docker-compose.yml
    if [[ ${1} == 0 ]];then
        sed -ri 's/(^DOCKER_IMAGE_RUNTIME_NAME=sdk-docker-runtime$)/\1-rel/g' .env
    elif [[ ${1} == 1 ]];then
        sed -ri 's/(^DOCKER_IMAGE_RUNTIME_NAME=sdk-docker-runtime$)/\1-dbg/g' .env
    fi
    # download arene-ivi-misc-configs.tar
    local FTP_DOCKER_URL=${FTP_SDK_DOCKER_URL}
    if [[ ${2} ]];then
        if [[ ${#2} == 6 ]];then
            ver=${2}
        elif [[ ${#2} -ge 8 ]];then
            ver=${2%.*}
            FTP_DOCKER_URL="${FTP_SDK_DOCKER_URL}/${2}"
        else
            log "[ERROR ] [${2}] is a wrong version number !!!."
            return 1
        fi
        log "[INFO ] version number is [${ver}] !!!."
        log "[INFO ] FTP_SDK_DOCKER_URL is [${FTP_DOCKER_URL}] !!!."
        HAL_DOCKER_NAME=sdk-docker-hal-mock_${ver}.tar.gz
        if [[ ${1} == 0 ]];then
            RUNTIME_DOCKER_NAME=sdk-docker-runtime-rel_${ver}.tar.gz
        elif [[ ${1} == 1 ]];then
            RUNTIME_DOCKER_NAME=sdk-docker-runtime-dbg_${ver}.tar.gz
        else
            RUNTIME_DOCKER_NAME=sdk-docker-runtime_${ver}.tar.gz
        fi
        sed -ri "4s#(DOCKER_IMAGE_HAL_TAG=).*#\1${ver}#g" ./.env
        sed -ri "6s#(DOCKER_IMAGE_RUNTIME_TAG=).*#\1${ver}#g" ./.env
    else
        HAL_DOCKER_NAME=sdk-docker-hal-mock_$(grep DOCKER_IMAGE_RUNTIME_TAG .env | cut -d = -f 2).tar.gz
        if [[ ${1} == 0 ]];then
            RUNTIME_DOCKER_NAME=sdk-docker-runtime-rel_$(grep DOCKER_IMAGE_HAL_TAG .env | cut -d = -f 2).tar.gz
        elif [[ ${1} == 1 ]];then
            RUNTIME_DOCKER_NAME=sdk-docker-runtime-dbg_$(grep DOCKER_IMAGE_HAL_TAG .env | cut -d = -f 2).tar.gz
        else
            RUNTIME_DOCKER_NAME=sdk-docker-runtime_$(grep DOCKER_IMAGE_HAL_TAG .env | cut -d = -f 2).tar.gz
        fi
    fi

    # download docker
    log "[NORMAL ] wget -O ${HAL_DOCKER_NAME} ${FTP_DOCKER_URL}/${HAL_DOCKER_NAME}."
    wget -O ${HAL_DOCKER_NAME} ${FTP_DOCKER_URL}/${HAL_DOCKER_NAME}
    if [[ $?  -ne 0 ]]; then
        log "[ERROR ] wget -O ${HAL_DOCKER_NAME} ${FTP_DOCKER_URL}/${HAL_DOCKER_NAME} ERROR !!!."
        return 1
    fi

    log "[NORMAL ] wget -O ${RUNTIME_DOCKER_NAME} ${FTP_DOCKER_URL}/${RUNTIME_DOCKER_NAME}."
    wget -O ${RUNTIME_DOCKER_NAME} ${FTP_DOCKER_URL}/${RUNTIME_DOCKER_NAME}
    if [[ $?  -ne 0 ]]; then
        log "[ERROR ] wget -O ${RUNTIME_DOCKER_NAME} ${FTP_DOCKER_URL}/${RUNTIME_DOCKER_NAME} ERROR !!!."
        return 1
    fi

    # remove old docker images and containers.
    log "[NORMAL ] docker stop name=sdk-hal-docker*."
    docker stop  $(docker ps -a -q --filter name=sdk-hal-docker*)

    log "[NORMAL ] docker rm name=sdk-hal-docker*."
    docker rm  $(docker ps -a -q --filter name=sdk-hal-docker*)

    log "[NORMAL ] docker rmi images=arene-os*/*."
    docker rmi $(docker images -q arene-os*/*)

    cd ${DOCKER_SCRIPT_PATH}
    sed -i '/^source ${SCRIPT_DIR}.*$/a\# set virtual display \
    Xvfb_info=$(ps -ef | grep Xvfb) \
    if [[ ${Xvfb_info} =~ "Xvfb :1 -screen 0 1280x720x24" ]]; then \
        echo "[info] Xvfb is exist info = [${Xvfb_info}]" \
    else \
        echo "[info] sh = [Xvfb :1 -screen 0 1280x720x24 &]" \
        nohup Xvfb :1 -screen 0 1280x720x24 & \
        sleep 30 \
    fi \
    export DISPLAY=:1.0 \
    export ARENE_IVI_HAL_MODE=MOCK' ${DOCKER_SCRIPT}
    sed -i '/function start_app() {$/a\    sleep 5' ${DOCKER_SCRIPT_PATH}/arene-common_docker.sh
    sed -i "s@sudo -v@echo \"${USER_PASSWORD}\" | sudo -S -v@" ${DOCKER_SCRIPT_PATH}/arene-common_docker.sh
    # sed -ri 's/(^\s+setup$)/#\1/g' ./arene-ivi_docker.sh
    # sed -ri 's/(^\s+xhost \+$)/#\1/g' ./arene-common_docker.sh
    
    # sed -i '/function start_app() {$/a\    export ARENE_IVI_HAL_MODE=MOCK' ./arene-common_docker.sh
    # # sed -i "/function start_app() {$/a\    export DISPLAY=:0" ./arene-common_docker.sh
    # sed -i "/function start_app() {$/a\    export DISPLAY=:1.0" ./arene-common_docker.sh
    # sed -i '/function start_app() {$/a\    fi' ./arene-common_docker.sh
    # sed -i "/function start_app() {$/a\        sleep 30" ./arene-common_docker.sh
    # sed -i '/function start_app() {$/a\        Xvfb :1 -screen 0 1280x720x24 &' ./arene-common_docker.sh
    # sed -i '/function start_app() {$/a\        echo "[info] sh = [Xvfb :1 -screen 0 1280x720x24 &]"' ./arene-common_docker.sh
    # sed -i '/function start_app() {$/a\    else' ./arene-common_docker.sh
    # sed -i '/function start_app() {$/a\        echo "[info] Xvfb is exist info = [${Xvfb_info}]"' ./arene-common_docker.sh
    # sed -i '/function start_app() {$/a\    if [[ ${Xvfb_info} ]]; then' ./arene-common_docker.sh
    # sed -i '/function start_app() {$/a\    local Xvfb_info=$(ps -ef | grep [X]vfb)' ./arene-common_docker.sh
    # sed -i '/function start_app() {$/a\    # set virtual display' ./arene-common_docker.sh
    # sed -ri 's@(^\s+elif grep -q "artifactory-ha.tmc-stargate.com" ~/.netrc > /dev/null 2>&1; then$)@#\1@g' ./arene-common_docker.sh
    # sed -ri 's@(^\s+fetch_docker_tarball$)@#\1@g' ./arene-common_docker.sh
    # sed -ri 's@(^\s+docker-compose pull$)@#\1@g' ./arene-common_docker.sh
    sync
    local Xvfb_info=$(ps -ef | grep [X]vfb)
    if [[ ${Xvfb_info} =~ "Xvfb :1 -screen 0 1280x720x24" ]]; then
        echo "[info] Xvfb is exist info = [${Xvfb_info}]"
    else
        echo "[info] sh = [Xvfb :1 -screen 0 1280x720x24 &]"
        Xvfb :1 -screen 0 1280x720x24 &
        sleep 30
    fi
    export DISPLAY=:1.0
    export HOW_TO_GET_DOCKER=3
    (${DOCKER_SCRIPT} install 2>&1)
    if [[ $?  -ne 0 ]]; then
        echo "[info] sh = [${DOCKER_SCRIPT} install] ERROR !!!"
        exit 1
    fi
}


function install_local_docker_env() {
    if [ -e ${DOCKER_TARGET_PATH} ]; then
        log "[NORMAL] ----[${DOCKER_TARGET_PATH}] existed !!!----"
    else
        log "[ERRPR] -- ${DOCKER_TARGET_PATH} does not exist !!!"
        return 1
    fi

    cd ${DOCKER_TARGET_PATH}
    HAL_DOCKER_NAME=sdk-docker-hal-mock_$(grep DOCKER_IMAGE_RUNTIME_TAG .env | cut -d = -f 2).tar.gz
    RUNTIME_DOCKER_NAME=sdk-docker-runtime_$(grep DOCKER_IMAGE_HAL_TAG .env | cut -d = -f 2).tar.gz

    # remove old docker images and containers.
    log "[NORMAL ] docker stop name=sdk-hal-docker*."
    docker stop  $(docker ps -a -q --filter name=sdk-hal-docker*)

    log "[NORMAL ] docker rm name=sdk-hal-docker*."
    docker rm  $(docker ps -a -q --filter name=sdk-hal-docker*)

    log "[NORMAL ] docker rmi images=arene-os*/*."
    docker rmi $(docker images -q arene-os*/*)

    local Xvfb_info=$(ps -ef | grep [X]vfb)
    if [[ ${Xvfb_info} ]]; then
        echo "[info] Xvfb is exist info = [${Xvfb_info}]"
    else
        echo "[info] sh = [Xvfb :1 -screen 0 1280x720x24 &]"
        Xvfb :1 -screen 0 1280x720x24 &
    fi
    export DISPLAY=:1.0
    (${DOCKER_SCRIPT} install 2>&1)
    if [[ $?  -ne 0 ]]; then
        echo "[info] sh = [${DOCKER_SCRIPT} install] ERROR !!!"
        exit 1
    fi
}

function ftp_scripts()
{
    log "[NORMAL] >>>>>>ftp script files start !<<<<<<"
#    ftp -ivn <<EOF
#    open ${FTP_IP} ${FTP_PORT}
#    user ${FTP_NAME} ${FTP_PASS}
#    binary
#    cd ${FTP_SCRIPT_PATH}
#    mget *.*
#    quit
#EOF
    cd $ARENE_WORKSPACE/scripts/internal
    wget ftp://tagl_ftp:tagl_ftp_211@10.1.43.211/scripts/internal/run_build
    cd $ARENE_WORKSPACE/scripts
    wget ftp://tagl_ftp:tagl_ftp_211@10.1.43.211/scripts/arene_build
    wget ftp://tagl_ftp:tagl_ftp_211@10.1.43.211/scripts/arene_env_config
    wget ftp://tagl_ftp:tagl_ftp_211@10.1.43.211/scripts/start-hal-docker
    log "[NORMAL] >>>>>>ftp script files  end !<<<<<<"
}

function install_new_environment() {
    if [ -e ${ENV_PATH} ]; then
        log "[ERROR] ----[${ENV_PATH}] existed !!!----"
    else
        mkdir ${ENV_PATH}
        log "[NORMAL] --create ${ENV_PATH} OK!"
    fi

    if [ -e $ARENE_WORKSPACE/src ]; then
        echo "[ERROR] ----[$ARENE_WORKSPACE/src] existed !!!----"
        echo ""
    else
        mkdir $ARENE_WORKSPACE/src
        mkdir $ARENE_WORKSPACE/src/github
        mkdir $ARENE_WORKSPACE/src/gitlab
        log "[NORMAL] --create $ARENE_WORKSPACE/src OK!"
        echo ""

        log "[NORMAL] --git clone arene-ivi-misc-configs start !"
        cd $ARENE_WORKSPACE/src/github
        git clone https://github.tmc-stargate.com/arene-os-services-cockpit-tmc-wa/arene-ivi-misc-configs.git
        cd $ARENE_WORKSPACE/src/github/arene-ivi-misc-configs
        git switch master

        # sed -ri 's/(^\s+setup$)/#\1/g' ./arene-ivi_docker.sh
        # sed -ri 's/(^\s+xhost \+$)/#\1/g' ./arene-common_docker.sh
        sed -ri 's/(^DOCKER_IMAGE_HAL_NAME=sdk-docker-hal$)/\1-mock/g' ./sdk-hal-docker/.env
        sed -ri "s@WORK_DIR=/home/arene/work@WORK_DIR=\$\{ARENE_WORKSPACE\}@g" ./sdk-hal-docker/.env
        sed -ri "s@DOCKER_IMAGE_HAL_TAG=v4.9.0@DOCKER_IMAGE_HAL_TAG=\$\{HAL_DOCKER_VERSION\}@g" ./sdk-hal-docker/.env
        sed -ri "s@DOCKER_IMAGE_RUNTIME_TAG=v4.9.0@DOCKER_IMAGE_RUNTIME_TAG=\$\{RUNTIME_DOCKER_VERSION\}@g" ./sdk-hal-docker/.env


        log "[NORMAL] >>>>>>install hal-runtime docker start !<<<<<<"
        install_hal_env
        log "[NORMAL] >>>>>>install hal-runtime docker  end  !<<<<<<"
        echo ""
        log "[NORMAL] --git clone arene-ivi-misc-configs  end  !"
        echo ""

        log "[NORMAL] --git clone arene-ivi-misc-poky-container start !"
        cd $ARENE_WORKSPACE/src/github
        git clone https://github.tmc-stargate.com/arene-os-services-cockpit-tmc-wa/arene-ivi-misc-poky-container.git
        cd $ARENE_WORKSPACE/src/arene-ivi-misc-poky-container
        git switch master
        #.docker.sh build
        #docker build \
	    #--build-arg http_proxy=${http_proxy} \
	    #--build-arg https_proxy=${https_proxy} \
	    #-t denso-ten/poky:ubuntu-20.04 .
        log "[NORMAL] --git clone arene-ivi-misc-poky-container  end  !"
        echo ""

    fi
}


function install_sdk_cdc() {
    if [ -e ${ENV_PATH} ]; then
        log "[NORMAL] ----[${ENV_PATH}] existed !!!----"
    else
        mkdir ${ENV_PATH}
        log "[NORMAL] --create ${ENV_PATH} OK!"
    fi
    cd ${ENV_PATH}
    if [[ ${1} ]];then
        local CDC_SDK_NAME="arene-toolchain-cockpit-cdc-*-genericx86-64.sh"   
        local SDK_NAME="arene-toolchain-cockpit-cdc-${1}-genericx86-64.sh"
        # download sdk
        log "[NORMAL ] rm -f ${CDC_SDK_NAME} ."
        rm -f ${CDC_SDK_NAME}

        log "[NORMAL ] wget -O ${SDK_NAME} ${FTP_BEV_RELEASE_URL}/${1}/${CDC_SDK_NAME} ."
        wget -O ${SDK_NAME} ${FTP_BEV_RELEASE_URL}/${1}/${CDC_SDK_NAME}
        if [[ $?  -ne 0 ]]; then
            log "[ERROR ] wget SDK File ERROR !!!."
            return 1
        fi
        chmod +x ${SDK_NAME}
        
        # remove old sdk
        log "[NORMAL ] rm -f sdk_cdc ."
        rm -rf ./sdk_cdc

        #install sdk
        cd ${WORK_PATH}
        log "[NORMAL ] ------ install ${ENV_PATH}/sdk_cdc start ------."
        docker run --rm -v ~:/home/hosthome:ro -v $(pwd):/workdir \
            --name poky-$(id -un) \
            --workdir=/workdir \
            ${DEFAULT_POKY_CONTAINER} ./env/${SDK_NAME} -y -d ./env/sdk_cdc

        grep -rni "version" ${ENV_PATH}/sdk_cdc/version-core2-64-arene-linux
        log "[NORMAL ] ------ install ${ENV_PATH}/sdk_cdc end ------."
    else
        log "[ERROR ] SDK_VERSION=[${SDK_VERSION}] ERROR !!!."
        exit 1
    fi
}

function install_docker_cdc() {
    if [ -e ${ENV_PATH} ]; then
        log "[NORMAL] ----[${ENV_PATH}] existed !!!----"
    else
        mkdir ${ENV_PATH}
        log "[NORMAL] --create ${ENV_PATH} OK!"
    fi

    if [[ ${2} ]];then
        log "[INFO ] version is [${2}] !!!."
        local runtime_docker_name="sdk-docker-runtime-dbg_*.tar.gz"
        local runtime_image_name="arene-cockpit-sdk-26bev-runtime-dbg"
        if [[ ${1} == 0 ]]; then
            runtime_docker_name="sdk-docker-runtime-rel_*.tar.gz"
            runtime_image_name="arene-cockpit-sdk-26bev-runtime-rel"
        fi
        if [ -e ${DOCKER_SCRIPT_PATH} ]; then
            log "[NORMAL] ----[${DOCKER_SCRIPT_PATH}] existed !!!----"
        else
            log "[NORMAL ] wget -O ${ENV_PATH}/${arene_ivi_misc_config_tar} ${FTP_SDK_DOCKER_URL}/${arene_ivi_misc_config_tar}."
            wget  -O ${ENV_PATH}/${arene_ivi_misc_config_tar} ${FTP_SDK_DOCKER_URL}/${arene_ivi_misc_config_tar}
            if [[ $?  -ne 0 ]]; then
                log "[ERROR ] wget -O ${ENV_PATH}/${arene_ivi_misc_config_tar} ${FTP_SDK_DOCKER_URL}/${arene_ivi_misc_config_tar} ERROR !!!."
                return 1
            fi
            tar -zxf ${ENV_PATH}/${arene_ivi_misc_config_tar} -C ${ENV_PATH}
            if [[ $?  -ne 0 ]]; then
                log "[ERROR ] tar -zxvf ${ENV_PATH}/${arene_ivi_misc_config_tar} ERROR !!!."
                return 1
            fi
            sed -i '/function start_app() {$/a\    sleep 5' ${DOCKER_SCRIPT_PATH}/arene-common_docker.sh
        fi
        
        # sed -ri "4s#(DOCKER_IMAGE_HAL_TAG=).*#\1${docker_ver}#g" ${DOCKER_BEV_PATH}/.env
        sed -i 's|DOCKER_REGISTRY=.*|DOCKER_REGISTRY="arene-os-services-cockpit-docker.artifactory-ha.tmc-stargate.com"|g' ${DOCKER_BEV_PATH}/.env
        sed -i 's|DOCKER_IMAGE_HAL_NAME=.*|DOCKER_IMAGE_HAL_NAME=sdk-docker-hal-cdc|g' ${DOCKER_BEV_PATH}/.env
        sed -ri "s|DOCKER_IMAGE_RUNTIME_NAME=.*|DOCKER_IMAGE_RUNTIME_NAME=${runtime_image_name}|g" ${DOCKER_BEV_PATH}/.env
        sed -ri "s|DOCKER_IMAGE_RUNTIME_TAG=.*|DOCKER_IMAGE_RUNTIME_TAG=${2}|g" ${DOCKER_BEV_PATH}/.env
        sed -ri "s|DOCKER_IMAGE_HAL_TAG=.*|DOCKER_IMAGE_HAL_TAG=${2}|g" ${DOCKER_BEV_PATH}/.env
        sed -i 's|DOCKER_PULL_SKIP=.*|DOCKER_PULL_SKIP=1|g' ${DOCKER_BEV_PATH}/.env
        sed -ri "s@WORK_DIR=~/hmifw_share@WORK_DIR=${WORK_PATH}@g" ${DOCKER_BEV_PATH}/.env
        sed -ri 's@(^\s+- /dev/dri:/dev/dri$)@#\1@g' ${DOCKER_BEV_PATH}/docker-compose.yml
        sed -ri 's|^\s+timeout:.*|      timeout: 30s|g' ${DOCKER_BEV_PATH}/docker-compose.yml
        sed -i "s@sudo -v@echo \"${USER_PASSWORD}\" | sudo -S -v@" ${DOCKER_SCRIPT_PATH}/arene-common_docker.sh

        source ${DOCKER_BEV_PATH}/.env
        rm -f ${DOCKER_BEV_PATH}/*.tar.gz
        # remove old docker images and containers.
        log "[NORMAL ] docker stop ${BEV_RUNTIMEL_CONTAINER_NAME}."
        docker stop ${BEV_RUNTIMEL_CONTAINER_NAME}
        log "[NORMAL ] docker stop ${BEV_HAL_CONTAINER_NAME}."
        docker stop ${BEV_HAL_CONTAINER_NAME}

        log "[NORMAL ] docker rm name=${BEV_RUNTIMEL_CONTAINER_NAME}."
        docker rm -f ${BEV_RUNTIMEL_CONTAINER_NAME}
        log "[NORMAL ] docker rm name=${BEV_HAL_CONTAINER_NAME}."
        docker rm -f ${BEV_HAL_CONTAINER_NAME}

        log "[NORMAL ] docker rmi images=${DOCKER_REGISTRY}/${DOCKER_IMAGE_RUNTIME_NAME}:*."
        docker rmi $(docker images -q ${DOCKER_REGISTRY}/${DOCKER_IMAGE_RUNTIME_NAME}:*)
        log "[NORMAL ] docker rmi images=${DOCKER_REGISTRY}/${DOCKER_IMAGE_HAL_NAME}:*."
        docker rmi $(docker images -q ${DOCKER_REGISTRY}/${DOCKER_IMAGE_HAL_NAME}:*)

        # download docker
        log "[NORMAL ] wget ${FTP_BEV_RELEASE_URL}/${2}/${runtime_docker_name}."
        wget -O ${DOCKER_BEV_PATH}/${DOCKER_IMAGE_RUNTIME_NAME}_${DOCKER_IMAGE_RUNTIME_TAG}.tar.gz ${FTP_BEV_RELEASE_URL}/${2}/${runtime_docker_name}
        if [[ $?  -ne 0 ]]; then
            log "[ERROR ] docker load ${FTP_BEV_RELEASE_URL}/${2}/${runtime_docker_name} ERROR !!!."
            return 1
        fi

        log "[NORMAL ] wget ${FTP_BEV_RELEASE_URL}/${2}/sdk-docker-hal-mock_*.tar.gz."
        wget -O ${DOCKER_BEV_PATH}/${DOCKER_IMAGE_HAL_NAME}_${DOCKER_IMAGE_HAL_TAG}.tar.gz ${FTP_BEV_RELEASE_URL}/${2}/sdk-docker-hal-mock_*.tar.gz
        if [[ $?  -ne 0 ]]; then
            log "[ERROR ] docker load ${FTP_BEV_RELEASE_URL}/${2}/sdk-docker-hal-mock_*.tar ERROR !!!."
            return 1
        fi

        docker load -i ${DOCKER_BEV_PATH}/${DOCKER_IMAGE_RUNTIME_NAME}_${DOCKER_IMAGE_RUNTIME_TAG}.tar.gz
        docker load -i ${DOCKER_BEV_PATH}/${DOCKER_IMAGE_HAL_NAME}_${DOCKER_IMAGE_HAL_TAG}.tar.gz
    else
        log "[ERROR ] CHECK IMAGES_VERSION=[${2}] ERROR !!!."
        exit 1
    fi

    local xvbf_flag=$(grep "Xvfb_info" ${DOCKER_BEV_SCRIPT})
    if [[ -z ${xvbf_flag} ]] ; then
        sed -i '/^source ${SCRIPT_DIR}.*$/a\# set virtual display \
        Xvfb_info=$(ps -ef | grep Xvfb) \
        if [[ ${Xvfb_info} =~ "Xvfb :1 -screen 0 1280x720x24" ]]; then \
            echo "[info] Xvfb is exist info = [${Xvfb_info}]" \
        else \
            echo "[info] sh = [Xvfb :1 -screen 0 1280x720x24 &]" \
            nohup Xvfb :1 -screen 0 1280x720x24 & \
            sleep 30 \
        fi \
        export DISPLAY=:1.0 \
        export ARENE_IVI_HAL_MODE=MOCK' ${DOCKER_BEV_SCRIPT}
    fi

    local Xvfb_info=$(ps -ef | grep [X]vfb)
    if [[ ${Xvfb_info} =~ "Xvfb :1 -screen 0 1280x720x24" ]]; then
        echo "[info] Xvfb is exist info = [${Xvfb_info}]"
    else
        echo "[info] sh = [Xvfb :1 -screen 0 1280x720x24 &]"
        Xvfb :1 -screen 0 1280x720x24 &
        sleep 30
    fi
    # export HOW_TO_GET_DOCKER=3
    (${DOCKER_BEV_SCRIPT} install ${DOCKER_SCRIPT_PATH}/sdk-hal-docker-cdc 2>&1)
    if [[ $?  -ne 0 ]]; then
        echo "[info] sh = [${DOCKER_BEV_SCRIPT} install] ERROR !!!"
        exit 1
    fi
}

########################################################
# main
########################################################
while :
do
    echo "#####################################################################"
    echo "#         Please select one of the options below to proceed         #"
    echo "# Options:                                                          #"
    echo "#         1   ARENE SDK install                                     #"
    echo "#         2   ARENE DOCKER install                                  #"
    echo "#         3   ARENE SDK install ver                                 #"
    echo "#         4   ARENE DOCKER install ver                              #"
    echo "#         5   ARENE SDK install local file                          #"
    echo "#         6   ARENE DOCKER install local file                       #"
    echo "#         7   CDC SDK install                                       #"
    echo "#         8   CDC DOCKER install                                    #"
    echo "#         0   exit                                                  #"
    echo "#####################################################################"

    read -p "Please enter your options:" options
    echo ""
    
    case ${options} in
            1)
                log "[NORMAL] >>>>>>Install sdk Start !<<<<<<"
                install_sdk
                log "[NORMAL] >>>>>>Install sdk  End  !<<<<<<"
                ;;
            2)
                read -p "Please enter docker type (0:release 1: debug 2:defult):" type
                log "[NORMAL] >>>>>>Install docker Start !<<<<<<"
                install_docker_env ${type}
                log "[NORMAL] >>>>>>Install docker  End  !<<<<<<"
                ;;
            3)
                read -p "Please enter your sdk version (eg:v7.0.0):" ver
                log "[NORMAL] >>>>>>Install sdk ${ver} Start !<<<<<<"
                install_sdk ${ver}
                log "[NORMAL] >>>>>>Install sdk ${ver}  End  !<<<<<<"
                ;;
            4)
                read -p "Please enter docker type (0:release 1: debug 2:defult):" type
                read -p "Please enter your docker version (eg:v7.0.0/v7.0.0.C):" ver
                log "[NORMAL] >>>>>>Install docker ${ver} Start !<<<<<<"
                install_docker_env ${type} ${ver}
                log "[NORMAL] >>>>>>Install docker ${ver} End  !<<<<<<"
                ;;
            5)
                log "[NORMAL] >>>>>>Install local sdk  Start !<<<<<<"
                install_local_sdk
                log "[NORMAL] >>>>>>Install local sdk End  !<<<<<<"
                ;;
            6)
                log "[NORMAL] >>>>>>Install local docker Start !<<<<<<"
                install_local_docker_env
                log "[NORMAL] >>>>>>Install local docker End  !<<<<<<"
                ;;
            7)
                read -p "Please enter your docker version (eg:v10.2.0-rc.3):" ver
                log "[NORMAL] >>>>>>Install sdk Start !<<<<<<"
                install_sdk_cdc ${ver}
                log "[NORMAL] >>>>>>Install sdk  End  !<<<<<<"
                ;;
            8)
                read -p "Please enter docker type (0:release 1: debug):" type
                read -p "Please enter your docker version (eg:v10.2.0-rc.3):" ver
                log "[NORMAL] >>>>>>Install docker Start !<<<<<<"
                install_docker_cdc ${type} ${ver}
                log "[NORMAL] >>>>>>Install docker  End  !<<<<<<"
                ;;
            0)
                log "[NORMAL] --exit 0--"
                exit 0
                ;;
            *)
                log "[ERROR ] ------wrong option = [ ${options} ]!!!------"
                ;;
    esac
done
